package com.boot.security.model;

import com.baomidou.mybatisplus.annotation.*;
import com.boot.json.deserializer.StringToLongDeserializer;
import com.boot.persistence.model.BaseModel;
import com.boot.persistence.model.LogicDeleteModel;
import com.boot.persistence.model.ReadOnlyModel;
import com.boot.persistence.validate.annotation.Delete;
import com.boot.persistence.validate.annotation.Insert;
import com.boot.persistence.validate.annotation.Update;
import com.boot.persistence.validate.group.DeleteGroup;
import com.boot.persistence.validate.group.InsertGroup;
import com.boot.persistence.validate.group.UpdateGroup;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.time.LocalDateTime;

@Data
@EqualsAndHashCode
@SuperBuilder
@NoArgsConstructor
@Insert(groups = InsertGroup.class)
@Update(groups = UpdateGroup.class)
@Delete(groups = DeleteGroup.class)
public class SystemModel implements BaseModel<Long>, LogicDeleteModel, ReadOnlyModel {
    /**
     * @description 主键
     * @date 2020/2/9 17:44
     **/
    @NotNull(groups = UpdateGroup.class)
    @TableId(type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    @JsonDeserialize(using = StringToLongDeserializer.class)
    protected Long id;

    /**
     * @description 创建时间
     * @date 2020/2/9 17:44
     **/
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT)
    protected LocalDateTime createTime;

    /**
     * @description 修改时间
     * @date 2020/2/9 17:45
     **/
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected LocalDateTime updateTime;

    /**
     * @description 创建人
     * @date 2020/2/9 17:45
     **/
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT)
    protected String createUser;

    /**
     * @description 修改人
     * @date 2020/2/9 17:46
     **/
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected String updateUser;

    /**
     * @description 删除标志，0,false:正常 1,true:已删除
     * @date 2020/2/9 17:46
     **/
    @JsonIgnore
    @TableLogic
    protected Boolean deleted;

    /**
     * @description 只读标志，0,false:可写 1,true:只读
     * @date 2020/2/9 17:46
     **/
    @Null(groups = {UpdateGroup.class, DeleteGroup.class})
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    protected Boolean readOnly;

    @Override
    public Long pk() {
        return id;
    }

    @Override
    public void setPk(Long pk) {
        id = pk;
    }

    @Override
    public Boolean deleted() {
        return deleted;
    }

    @Override
    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    @Override
    public Boolean readOnly() {
        return readOnly;
    }

    @Override
    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }
}
